package conchmusic.conchmusicclient.popupWindow;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.PopupWindow;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import conchmusic.conchmusicclient.R;
import conchmusic.conchmusicclient.dao.ClientControl;
import conchmusic.conchmusicclient.dao.MyDBHelp;
import conchmusic.conchmusicclient.pojo.Song;
import conchmusic.conchmusicclient.util.DeleteSong;
import conchmusic.conchmusicclient.util.InitPlayBar;
import conchmusic.conchmusicclient.view.PlayBar;

/**
 * Created by Administrator on 2017/4/8.
 */

public class SelectPicPopupWindow extends PopupWindow {
    private View mMenuView;
    private Button delete;
    private Button nextSong;
    private Button cancel;
    private int position;
    private BaseAdapter listViewAdapter;
    private Context context;
    private List<Song> listItems;
    private Button okButton;
    private Button cancleButton;
    private CheckBox checkBox;
    private Dialog dialog;
    private ClientControl clientControl;
    private MyDBHelp myDBHelp;
    private DeleteSong deleteSong;
    private InitPlayBar initPlayBar;
    private PlayBar playBar;
    private Button btn_play;
    public SelectPicPopupWindow(BaseAdapter listViewAdapter, Activity context, int position, List<Song> listItems) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.select_pic_popup_window, null);
        this.listItems=listItems;
        this.position=position;
        this.listViewAdapter=listViewAdapter;
        this.context=context;
        btn_play=(Button)context.findViewById(R.id.playbar_play);
        if(context.getLocalClassName().equals("activity.SearchActivity")){
            playBar=(PlayBar)context.findViewById(R.id.local_search_playbar);
        }else{
            playBar=(PlayBar)context.findViewById(R.id.localMusic_playbar);
        }

        initPlayBar=new InitPlayBar(playBar,context);
        myDBHelp=new MyDBHelp(context,"demon",null,1);
        clientControl=new ClientControl(myDBHelp);
        delete = (Button) mMenuView.findViewById(R.id.delete);
        nextSong = (Button) mMenuView.findViewById(R.id.nextSong);
        cancel = (Button) mMenuView.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        delete.setOnClickListener(itemsOnClick);
        nextSong.setOnClickListener(itemsOnClick);
        this.setContentView(mMenuView);
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });

    }
    private View.OnClickListener itemsOnClick = new View.OnClickListener(){

        public void onClick(View v) {
            dismiss();
            switch (v.getId()) {
                case R.id.nextSong:
                    String[] list=new String[listItems.size()];
                    for(int i=0;i<listItems.size();i++){
                        list[i]=Integer.toString(listItems.get(i).getLocalSongId());
                    }
                    initPlayBar.itemInitPlaybar();
                    btn_play.setBackgroundResource(R.mipmap.ic_pause);
                    Intent intent=new Intent();
                    intent.putExtra("action","play");
                    intent.putExtra("posit",position);
                    intent.putExtra("songList",list);
                    intent.setAction("music2");
                    intent.setPackage(context.getPackageName());
                    context.startService(intent);
                    //播放下一首
                    break;
                case R.id.delete:
                    dialog=new AlertDialog.Builder(context).create();
                    dialog.show();
                    dialog.getWindow().setContentView(R.layout.local_sure_popup);
                    WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                    WindowManager windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
                    int width = windowManager.getDefaultDisplay().getWidth();//屏幕宽度
                    int height = windowManager.getDefaultDisplay().getHeight();
                    params.width = (int)(width*0.8);
                    params.height = (int)(height*0.4);
                    dialog.getWindow().setAttributes(params);
                    okButton=(Button) dialog.getWindow().findViewById(R.id.dialog_ok);
                    cancleButton=(Button) dialog.getWindow().findViewById(R.id.dialog_cancel);
                    checkBox=(CheckBox)dialog.getWindow().findViewById(R.id.check_delete_single_popup) ;
                    okButton.setOnClickListener(sureDeleteListener);
                    cancleButton.setOnClickListener(sureDeleteListener);
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            checkBox.setChecked(isChecked);
                        }
                    });
                    break;
                default:

                    break;
            }
        }
    };

    private View.OnClickListener sureDeleteListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialog_ok:
                    if(checkBox.isChecked()){
                        //删除本地
                        Song song=listItems.get(position);
                        clientControl.deleteOriginSong(song.getLocalSongId(),1);
                        listItems.remove(position);
                        deleteSong=new DeleteSong(song.getLocalSongUrl(),song.getLocalPictureUrl(),song.getLocalLyricUrl());
                        deleteSong.deleteSong();

                        listViewAdapter.notifyDataSetChanged();
                        Log.i("checkBox","check");
                    }else {
                        //不删除本地
                        Song song=listItems.get(position);
                        clientControl.deleteSong(song.getLocalSongId(),1);
                        listItems.remove(position);
                        listViewAdapter.notifyDataSetChanged();
                        Log.i("checkBox","notcheck");
                    }
                    try {
                        Method method=context.getClass().getMethod("notifyLocalMusic");
                        method.invoke((Object)context);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    dialog.cancel();
                    Log.i("sureDelete","click");
                    break;
                case R.id.dialog_cancel:
                    Log.i("notSureDelete","click");
                    dialog.cancel();
                    break;
            }
        }

    };


}